//
//  FMUserViewController.m
//  PsychologyFM
//
//  Created by zhang on 16/1/11.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//


#import "FMUserLoginedView.h"
#import "FMUserUnLoginView.h"
#import "FMUserViewController.h"
#import "FMLoginViewController.h"
#import "FMLoginManager.h"
#import <UIImageView+WebCache.h>
#import <FMDB.h>
#import "FMDataBaseModel.h"

@interface FMUserViewController ()
@property (nonatomic, strong)UIImageView *bgImage;
@property (nonatomic, strong)UIImageView *iconImage;
@property (nonatomic, strong)UIButton *loginButton;
@property (nonatomic, strong)UILabel *label;
@property (nonatomic, strong)NSNumber *like;
@property (nonatomic,strong) FMDatabase *db;

@property (nonatomic,strong) NSMutableArray *dataBaseArray;

@property (nonatomic,strong) FMDataBaseModel *databaseModel;
//@property (nonatomic, strong)UILabel *
@end

@implementation FMUserViewController

- (void)dealloc
{
    //  移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"userHadLogined" object:nil];
    
}

- (void)loadView
{
    [super loadView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //  根据数据库查找
    //  创建我们的数据库的路径 同时拼接数据库的文件名称
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"FMDataBase.db"];
    //  根据路径去创建一个数据库
    _db = [FMDatabase databaseWithPath:path];

//    NSLog(@"%@",path);
    
    //  调用open方法 打开成功返回YES 失败 返回NO
    if ([_db open]) {
        NSLog(@"数据库打开成功");
    }else
    {
        NSLog(@"数据库打开失败");
    }
    //  分配空间
    _dataBaseArray = [NSMutableArray array];
    
    //  查找取值
    //  执行在查找的操作 调用的方法是 executeQuery 参数是SQL的查找语句
    //  会返回一个 FMResultSet 类型的值
    FMResultSet *result = [_db executeQuery:@"SELECT * FROM UserInformationlist"];
    while ([result next]) {
        //(isLogin text,avatar text, user_id text, introduce text, nickname text, qq_openid text, renren_openid text, sina_openid text, expire text, token text)
        NSString *isLogin = [result stringForColumn:@"isLogin"];
        NSString *avatar = [result stringForColumn:@"avatar"];
        NSString *user_id = [result stringForColumn:@"user_id"];
        NSString *introduce = [result stringForColumn:@"introduce"];
        NSString *nickname = [result stringForColumn:@"nickname"];
        NSString *qq_openid = [result stringForColumn:@"qq_openid"];
        NSString *renren_openid = [result stringForColumn:@"renren_openid"];
        NSString *sina_openid = [result stringForColumn:@"sina_openid"];
        NSString *expire = [result stringForColumn:@"expire"];
        NSString *token = [result stringForColumn:@"token"];
        
        NSLog(@"%@",isLogin);
        //  一定要分配空间
        _databaseModel = [[FMDataBaseModel alloc]init];
        _databaseModel.isLogin = isLogin;
        _databaseModel.avatar = avatar;
        _databaseModel.user_id = user_id;
        _databaseModel.nickname = nickname;
        _databaseModel.qq_openid = qq_openid;
        _databaseModel.renren_openid = renren_openid;
        _databaseModel.sina_openid = sina_openid;
        _databaseModel.introduce = introduce;
        _databaseModel.expire = expire;
        _databaseModel.token = token;
        
        [_dataBaseArray addObject:_databaseModel];
    }
    if (_dataBaseArray.count == 0) {
        FMUserUnLoginView *Myview = [[FMUserUnLoginView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        [Myview.loginButton addTarget:self action:@selector(loginButton:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [self.view addSubview:Myview];
    }else{
    FMDataBaseModel *model = _dataBaseArray[0];
    //FMDataBaseModel *model = _databaseModel;
    NSString *isLogin = model.isLogin;
    if ([isLogin isEqualToString:@"YES"]) {
        FMUserLoginedView *view = [[FMUserLoginedView alloc]initWithFrame:self.view.bounds];
        //  给对应的空间赋值
        [view.iconImgView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:nil completed:nil];
        view.nickNameIConLabel.text = model.nickname;
        view.introduceLabel.text = model.introduce;
        
        [self.view addSubview:view];
    }else
    {
    FMUserUnLoginView *Myview = [[FMUserUnLoginView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [Myview.loginButton addTarget:self action:@selector(loginButton:) forControlEvents:(UIControlEventTouchUpInside)];
        
        
        
        
    [self.view addSubview:Myview];
    }
    
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //  注册通知
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(notification:) name:@"userHadLogined" object:nil];
    [center addObserver:self selector:@selector(userQuitNotification:) name:@"userQuit" object:nil];


}

- (void)loginButton:(UIButton *)sender
{
    FMLoginViewController *loginVC = [[FMLoginViewController alloc] init];
    loginVC.BBBock = ^void(FMLoginSucceedModel *succeedModel){
        
        FMUserLoginedView *view = [[FMUserLoginedView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        //  给对应控件赋值
        [view.iconImgView sd_setImageWithURL:[NSURL URLWithString:succeedModel.data[@"avatar"]] placeholderImage:nil completed:nil];
        view.nickNameIConLabel.text = succeedModel.data[@"nickname"];
        view.introduceLabel.text = succeedModel.data[@"introduce"];
        
        
        
        
        
        
        self.view = view;
    };
    
    [self.navigationController pushViewController:loginVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
//    FMLoginViewController *loginVC = [[FMLoginViewController alloc] init];
//    loginVC.BBBock = ^void(NSNumber *num){
//        _like = num;
//        NSLog(@"like = %@",_like);
//    };
    self.title = @"我的";
    self.navigationController.navigationBar.hidden = YES;
//    if ([_like compare:_isLogin]) {
//        NSLog(@"YES");
//    }
//    else
//    {
//        NSLog(@"NO");
//    }
}

- (void)notification:(NSNotification *)notification
{
    FMUserLoginedView *view = [[FMUserLoginedView alloc]initWithFrame:self.view.bounds];
    //  给对应控件赋值
    NSDictionary *dic = notification.userInfo;
    [view.iconImgView sd_setImageWithURL:[NSURL URLWithString:dic[@"data"][@"avatar"]] placeholderImage:nil completed:nil];
    view.nickNameIConLabel.text = dic[@"data"][@"nickname"];
    view.introduceLabel.text = dic[@"data"][@"introduce"];
    
    //  取出手机的token值
    NSString *tokenStr = dic[@"token"];
    
    //  根据传过来的手机token值请求数据给私信通知或者我的金币赋值
    [PKRequestManager requestWithUrlString:[NSString stringWithFormat:@"http://bapi.xinli001.com/fm2/myuserinfo.json/?key=c0d28ec0954084b4426223366293d190&token=%@",tokenStr] parDic:nil method:GET finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        view.coinSizeLabel.text = [NSString stringWithFormat:@"%@",dic[@"data"][@"bitcoin"]];
        
    } error:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    self.view = view;
}

- (void)userQuitNotification:(NSNotification *)notification
{
    FMUserUnLoginView *Myview = [[FMUserUnLoginView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [Myview.loginButton addTarget:self action:@selector(loginButton:) forControlEvents:(UIControlEventTouchUpInside)];

    self.view = Myview;

}

@end
